/*
 * Copyright 2010 Rajendra Patil
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.slideshare4j.transport;

import java.io.UnsupportedEncodingException;
import java.net.Proxy;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.googlecode.slideshare4j.util.SlideShare4jUtils;

public abstract class AbstractTransport implements ITransport {

	private final String apiKey;
	private final String sharedSecret;
	
	private Proxy proxy;
	
	public Proxy getProxy() {
		return proxy;
	}
	
	public void setProxy(Proxy proxy) {
		this.proxy = proxy;
		
	}
	
	protected AbstractTransport(String apiKey, String sharedSecret) {
		this.apiKey = apiKey;
		this.sharedSecret = sharedSecret;
		
	}
	
	protected Map<String,String> addApiAuthParams(Map<String,String> params) throws NoSuchAlgorithmException{
		if(params == null){
			params = new HashMap<String, String>();
		}
		Date now = new Date();
        String ts = Long.toString(now.getTime() / 1000);
        params.put("ts", ts);
        params.put("hash", SlideShare4jUtils.getSlideshareHash(ts,this.sharedSecret));
        params.put("api_key", this.apiKey);
		return params;
	}
	
	protected String getURL(String URL, Map<String,String> params) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		return SlideShare4jUtils.buildUrl(URL,addApiAuthParams(params));
	}

}
